package org.javaforever.cookieshop.controller;

import java.util.List;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class AdminTypeCustController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected CustOrderService orderService;
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected UserService userService;
        
    @GetMapping("/admin/type_list")
    public String adminTypeList(@RequestParam(value = "type", defaultValue = "0") Long type, @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{   
        List<TypeBean> list= typeService.listActiveTypes();
        model.addAttribute("list", list);
        model.addAttribute("typeList",list);
        return "/admin/type_list";
    }
    
    @GetMapping("/admin/type_delete")
    public String adminTypeDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        typeService.deleteType(id);
	        return "redirect:/admin/type_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "redirect:/admin/type_list";
	    }
	}
    
    @GetMapping("/admin/type_softdelete")
    public String adminTypeSoftDelete(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    try {
	        typeService.softDeleteType(id);
	        return "redirect:/admin/type_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "redirect:/admin/type_list";
	    }
	}
    
    @PostMapping("/admin/type_add")
    public String adminTypeSoftDelete(@RequestParam(value = "name", required = true) String name, Model model) throws Exception{   
	    try {
	    	TypeBean type = new TypeBean();
	    	type.setName(name);
	    	type.setActive(true);
	    	typeService.addType(type);
	    	return "redirect:/admin/type_list";
	    } catch (FileUploadException e) {
	        e.printStackTrace();
	        return "redirect:/admin/type_list";
	    }
	}
    
    @GetMapping("/admin/go_type_edit")
    public String adminGoTypeEdit(@RequestParam(value = "id", required = true) Long id, Model model) throws Exception{   
	    TypeBean t = typeService.findTypeById(id);
	    model.addAttribute("t", t);
	    return "/admin/type_edit";
	}
    
    @PostMapping("/admin/type_edit")
    public String adminTypeEdit(@RequestParam(value = "id", required = true) Long id,@RequestParam(value = "name", required = true) String name, Model model) throws Exception{   
	    TypeBean t = new TypeBean();
	    t.setId(id);
	    t.setName(name);
	    t.setActive(true);
	    typeService.updateType(t);
	    return "redirect:/admin/type_list";
	}
}
